package com.emapp.lccode.base

import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.mylhyl.circledialog.BaseCircleDialog
import com.mylhyl.circledialog.CircleDialog

/**
 */

//loading框
//private var loadingDialog: MaterialDialog? = null
var builder = CircleDialog.Builder()

/**
 * 打开等待框
 */
fun AppCompatActivity.showLoadingExt(message: String = "请求网络中") {
    if (!this.isFinishing) {
//        if (loadingDialog == null) {
//            loadingDialog = MaterialDialog(this)
//                    .cancelable(true)
//                    .cancelOnTouchOutside(false)
//                    .cornerRadius(12f)
//                    .customView(R.layout.layout_custom_progress_dialog_view)
//                    .lifecycleOwner(this)
//            loadingDialog?.getCustomView()?.run {
//                this.findViewById<TextView>(R.id.loading_tips).text = message
//                this.findViewById<ProgressBar>(R.id.progressBar).indeterminateTintList = SettingUtil.getOneColorStateList(this@showLoadingExt)
//            }
//        }
//        loadingDialog?.show()
        //if (builder == null) {
        builder
            //.setTitle("提示")
            // .setSubTitle("副提示语") // .setTypeface(typeface)
            .setWidth(0.6f)
            .setLottieAnimation("loading/loading2.json") // 有文件夹的
            .setLottieLoop(true)
            .playLottieAnimation()
            .setLottieText(message)
            .show(supportFragmentManager)
    }
}

/**
 * 打开等待框
 */
fun Fragment.showLoadingExt(message: String = "请求网络中") {
    activity?.let {
        if (!it.isFinishing) {
//            if (loadingDialog == null) {
//                loadingDialog = MaterialDialog(it)
//                    .cancelable(true)
//                    .cancelOnTouchOutside(false)
//                    .cornerRadius(12f)
//                    .customView(R.layout.layout_custom_progress_dialog_view)
//                    .lifecycleOwner(this)
//                loadingDialog?.getCustomView()?.run {
//                    this.findViewById<TextView>(R.id.loading_tips).text = message
//                    this.findViewById<ProgressBar>(R.id.progressBar).indeterminateTintList = SettingUtil.getOneColorStateList(it)
//                }
//            }
//            loadingDialog?.show()
            //   if (builder == null) {
            builder
                //.setTitle("提示")
                // .setSubTitle("副提示语") // .setTypeface(typeface)
                .setWidth(0.6f)
                .setLottieAnimation("loading/loading2.json") // 有文件夹的
                .setLottieLoop(true)
                .playLottieAnimation()
                .setLottieText(message)

                // .show(parentFragmentManager)
                // }
                .show(parentFragmentManager)
        }
    }
}

/**
 * 关闭等待框
 */
fun Activity.dismissLoadingExt() {
//    loadingDialog?.dismiss()
//    loadingDialog = null
    builder.dismiss()
}

/**
 * 关闭等待框
 */
fun Fragment.dismissLoadingExt() {
//    loadingDialog?.dismiss()
//    loadingDialog = null
    builder.dismiss()
}
